$(function() {
    voice.init();
    $("#Search").button({
        text: true,
        icons: {
            primary: "ui-icon-search"
        }
    });

    $(window).hashchange(voice._Search);
});

var sortUi = {
    nowMoveingOjbectPrevId: 0
    , _Sortable: function() {
        $(".voicecontant tbody").sortable({
            stop: sortUi._AfterSorting
            , start: sortUi._Start
        }).disableSelection();
    }
    , _AfterSorting: function(e, ui) {
        var rangeFrom, rangeTo, newObjectPrevId, newId, currentSn;
        newObjectPrevId = (ui.item.prev().length != 0) ? ui.item.prev().attr('sortid') : 0;
        newObjectPrevId = parseInt(newObjectPrevId, 10);
        currentSn = parseInt(ui.item[0].id, 10);
        if (sortUi.nowMoveingOjbectPrevId == newObjectPrevId) {
            return;
        }

        var gape = 0;

        if (newObjectPrevId == 0) {
            rangeFrom = parseInt(ui.item.next().attr('sortid'), 10);
            rangeTo = sortUi.nowMoveingOjbectPrevId;
            newId = rangeFrom;
            gape = 1;
        } else {
            if (newObjectPrevId > sortUi.nowMoveingOjbectPrevId) {
                rangeFrom = sortUi.nowMoveingOjbectPrevId + 1;
                rangeTo = newObjectPrevId;
                newId = newObjectPrevId;
                gape = -1;
            } else {
                rangeFrom = newObjectPrevId + 1;
                rangeTo = sortUi.nowMoveingOjbectPrevId;
                newId = newObjectPrevId + 1;
                gape = 1;
            }
        }

        var param = { SortingFrom: rangeFrom, SortingTo: rangeTo, VoiceSn: currentSn, NewSortingID: newId };
        var cb = function() { window.parent.location.reload(); }
        //reset sorting number
        $('#bolisting tr').each(
            function() {
                var sortid = parseInt($(this).attr('sortid'), 10);
                var sn = parseInt($(this).attr('id'), 10);
                if (sortid >= rangeFrom && sortid <= rangeTo) {
                    $(this).attr('sortid', sortid + gape);
                }
                if (sn == currentSn) {
                    $(this).attr('sortid', newId);
                }
            }
        );

        utility.service('VoiceService/ReSorting', param, 'POST');
    }
    , _Start: function(e, ui) {
        if (ui.item.prev().length != 0) {
            sortUi.nowMoveingOjbectPrevId = ui.item.prev().attr('sortid');
            sortUi.nowMoveingOjbectPrevId = parseInt(sortUi.nowMoveingOjbectPrevId, 10);
        } else {
            sortUi.nowMoveingOjbectPrevId = 0;
        }
    }
}



var voice =
{
    prevOjbectId: 0,
    init: function() {
        //binding events.
        $('#content a[delsn]').live("click", function() {
            voice.delConfirm($(this).attr("delsn"));
        });

        $('#content a[convsn]').live("click", function() {
            voice.doConvert($(this).attr("convsn"));
        });

        $('#content a[playsn]').live("click", function() {
            voice.doPlayVoice($(this).attr("playsn"));
        });

        $('#pageing').change(voice.Search);
        $("#Search").click(function() {
            $("#pageing").val('1');
            voice.Search();
        });
        //set default values.
        utility.formatPaging($('#pageing'), 1, 1);
        //set init action
        if (utility.getObjFromHash() != null) {
            voice._Search();
        }
        //set default event.
        Utils.textBoxsOnEnter(function() { $("#Search").trigger('click'); });
    }
    , doPlayVoice: function(sn) {
        var url = '/Image/Wav?fn=s' + sn + '.wav';
        $('#embed_player').remove();
        $('#content').append('<embed id="embed_player" src="' + url + '" autostart="true" hidden="true"></embed>');
    }
    , doConvert: function(sn) {
        var param = { sn: sn };
        var cb = function() { window.parent.location.reload(); }
        utility.ajaxext("VoiceService/VoiceConvert", param, "POST", cb);
    }
    , delConfirm: function(sn) {
        voice.currentSN = parseInt(sn, 10);
        var objfun = function() {
            voice.doDel(sn);
        }
        utility.showPopUp(m.Msg0003, 3, objfun);
    }
    , doDel: function(x) {
        var param = { sn: x };
        var cb = function() { window.parent.location.reload(); }
        utility.ajaxext("VoiceService/Delete", param, "POST", cb);
    }
    , Search: function() {
        var param = { page: $('#pageing').val(), searchkey: $('#searchkey').val(), categorySn: $('#categorySn').val() };
        utility.setObjToHash(param);
    }
    , _Search: function() {
        var param = utility.addPageToPara(utility.getObjFromHash());
        var chks = new Array("page");
        var hasparam = utility.isHashVerfied(chks, param);
        if (hasparam) {
            utility.service('VoiceService/Search', param, 'POST', function(data) {
                if (data.code > 0) {
                    if (data.code > 1) {
                        $('#bolisting').html('<tr><td colspan="7" class="align-center">' + data.msg + '</td></tr>');
                        utility.formatPaging($('#pageing'), 1, 1);
                    } else {
                        var currentpage = (parseInt(param['page'], 10) < data.tp) ? parseInt(param['page'], 10) : data.tp;
                        var templatepara = { 'pList': data.d, 'g': g, 'pg': currentpage };
                        utility.template("Voice/Voicelisting.html", function(template) {
                            $('#bolisting').html(template.process(templatepara));
                            utility.formatPaging($('#pageing'), data.tp, currentpage);
                            PageInitial.InitialPopupDiv();
                            sortUi._Sortable();
                        }, "Voicelisting");
                    }
                } else {
                    utility.showPopUp(data.msg, 1);
                }
            });
        } else {
            this.Search();
        }
    }
};
